package com.cs342.uwosh.cloudycontacts.tasks.db;

import java.util.ArrayList;
import java.util.List;

import com.cs342.uwosh.cloudycontacts.Contact;
import com.cs342.uwosh.cloudycontacts.MainActivity;
import com.cs342.uwosh.cloudycontacts.R;
import com.cs342.uwosh.cloudycontacts.SqlConnect;


import android.os.AsyncTask;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;


public class GetAllContactTask extends AsyncTask<Object, Object, ArrayList<Contact>>{

	
	@Override
	protected ArrayList<Contact> doInBackground(Object... arg0) {

		SqlConnect db = MainActivity.db;
		
		return (ArrayList<Contact>)db.getAll();
	}
	
	/**
	 * 
	 */
	@Override
	protected void onPostExecute(ArrayList<Contact> result)
	{
		ArrayList<Contact> contacts = result;

        for(Contact c: contacts)
        {
        	MainActivity.allContacts.put(""+c.getId(), c);
        }
        
        if (MainActivity.CURRENT_THEME.equals(MainActivity.THEME_DEFAULT))
        {
            MainActivity.contactListAdapter = new ArrayAdapter<Contact>(MainActivity.mainContext, R.layout.list_item_theme_default, R.id.contactItem, contacts);
        }
        else if (MainActivity.CURRENT_THEME.equals(MainActivity.THEME_ONE))
        {
            MainActivity.contactListAdapter = new ArrayAdapter<Contact>(MainActivity.mainContext, R.layout.list_item_theme1_default, R.id.contactItem, contacts);
        }
        else if (MainActivity.CURRENT_THEME.equals(MainActivity.THEME_TWO))
        {
            MainActivity.contactListAdapter = new ArrayAdapter<Contact>(MainActivity.mainContext, R.layout.list_item_theme2_default, R.id.contactItem, contacts);
        }
        else if (MainActivity.CURRENT_THEME.equals(MainActivity.THEME_THREE))
        {
            MainActivity.contactListAdapter = new ArrayAdapter<Contact>(MainActivity.mainContext, R.layout.list_item_theme3_default, R.id.contactItem, contacts);
        }
        
        MainActivity.listOfContacts.setAdapter(MainActivity.contactListAdapter);

	}
}